;The following program shows how to connect the 8051 serial port to the COM port of the IBM PC. 
;P1 and P2 of the 8051 are connected to different devices for transmitting and receiving data, respectively. 
;This program will do the following process:

;(a)	send to the PC the message "Ready",
;(b)	receive any data sent by the PC and put it to P1, and
;(c)	get data from device connected to P2 and send it to the PC serially.

;The program will perform part (a) once, but parts (b) and (c) continuously.



#include <Sfr51.inc>

	ORG 0

    MOV P2, #0ffh           ;make P2 an input port
	MOV TMOD, #20h          ;timer 1, mode 2(auto-reload)
	MOV TH1, #0fdh          ;9600 baud rate
	MOV SCON, #50h          ;8-bit,1 stop, REN enabled
	SETB TR1                ;start timer 1
	MOV DPTR, #DISPLAY      ;load pointer for message
 
	H_1:
	CLR A
	MOVC A, @A+DPTR         ;get the character
	JZ B_1                  ;if last character get out
	ACALL SEND		        ;otherwise call transfer
	INC DPTR                ;next one
	SJMP H_1                ;stay in loop indefinitly
	
	B_1:
	MOV A, P2               ;read data on INT1
	ACALL SEND              ;transfer it serially
	ACALL RECV              ;get the serial data
	MOV P1, A               ;display it on port 1
	SJMP B_1                ;stay in loop indefinitly

	;serial data transfer . ACC has the data

	SEND:
	MOV SBUF, A            ;load the data

	H_2:
	JNB TI, H_2            ;stay here until last bit gone
	CLR TI                 ;get ready for next character
	RET                    ;return to caller

	;receive data serially in ACC
	RECV:
	JNB RI, RECV           ;wait here for character
	MOV A, SBUF            ;save it in ACC
	CLR RI                 ;get ready for next character
	RET                    ;return to caller
   
   ;the message
	DISPLAY:  DB "READY"

    END